package com.jframe.app.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.app.model.DataSourse;
import com.jframe.app.service.DataSourseService;
import com.jframe.sys.controller.utils.BaseAction;
import com.jframe.sys.controller.utils.TabmCache;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Tree;

public class DataSourseAction extends BaseAction {

	public void load(){
		int appId = StringUtil.parseInt(this.request.getParameter("appId"));
		DataSourseService dataSourseService = (DataSourseService)this.getBean("dataSourseService");	
		DataSourse dataSourse = dataSourseService.load("appId",appId);
		dataSourse.setPwd(StringUtil.decrypt(StringUtil.null2Space(dataSourse.getPwd())));
		this.print(JsonUtil.obj2Json(dataSourse));
	}
	
	public void save(){
		String obj = this.request.getParameter("obj");
		DataSourse dataSourse = (DataSourse) JsonUtil.json2Obj(obj,DataSourse.class);
		DataSourseService dataSourseService = (DataSourseService)this.getBean("dataSourseService");		
		dataSourse.setPwd(StringUtil.encrypt(StringUtil.null2Space(dataSourse.getPwd())));
		dataSourseService.save(dataSourse);
		this.print("true");
	}
	
	public void getRightTrees(){
		String json = TabmCache.getCombotreeJson("sm_sys_app");
		List<Tree> ls = JsonUtil.json2List(json, Tree.class);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();		
		for(Tree tree:ls){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", ""+tree.getId());
			map.put("name", tree.getText());
			list.add(map);
		}		
		this.print(JsonUtil.list2Json(list));
	}
	
}
